package com.heima.search.service.impl;

import com.heima.common.excpetion.CustException;
import com.heima.feign.behavior.BehaviorFeign;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.search.dtos.HistorySearchDTO;
import com.heima.model.search.dtos.UserSearchDTO;
import com.heima.model.search.pojos.ApUserSearch;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import com.heima.search.service.ApUserSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户搜索业务层
 * ES ThreadPool Mongo
 */
@Service("ApUserSearchService")
public class ApUserSearchServiceImpl implements ApUserSearchService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    BehaviorFeign behaviorFeign;

    /**
     * 异步插入用户搜索记录
     *
     * @param userSearchDto 用户搜索DTO
     */
    @Override
    @Async("taskExecutor")
    public void insert(UserSearchDTO userSearchDto) {
        //1 获取参数
        String searchWords = userSearchDto.getSearchWords();
        Integer entryId = userSearchDto.getEntryId();

        //2 查询行为实体
        ResponseResult<ApBehaviorEntry> behaviorResponse = behaviorFeign.findByUserIdOrEquipmentId(entryId, userSearchDto.getEquipmentId());
        if (!behaviorResponse.checkCode()) {
            CustException.cust(AppHttpCodeEnum.REMOTE_SERVER_ERROR);
        }
        // 获取数据
        ApBehaviorEntry apBehaviorEntry = behaviorResponse.getData();
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3 查询当前用户搜索的关键词是否存在
        ApUserSearch apUserSearch = mongoTemplate.findOne(Query.query(
                        Criteria.where("entryId").is(entryId)
                                .and("keyword").is(searchWords))
                , ApUserSearch.class);

        //3.1 存在则更新最新时间，更新状态
        if (apUserSearch != null) {
            apUserSearch.setCreatedTime(new Date());
            mongoTemplate.save(apUserSearch);
            return;
        }
        //3.2 不存在则新增
        apUserSearch = new ApUserSearch();
        apUserSearch.setCreatedTime(new Date());
        apUserSearch.setEntryId(String.valueOf(entryId));
        apUserSearch.setKeyword(searchWords);
        mongoTemplate.save(apUserSearch);
    }

    /**
     * 查询用户搜索历史记录  显示10条
     *
     * @param userSearchDto
     * @return
     */
    @Override
    public ResponseResult findUserSearch(UserSearchDTO userSearchDto) {
        if (userSearchDto.getMinBehotTime() == null) {
            userSearchDto.setMinBehotTime(new Date());
        }
        int size = 10;
        ApUser user = AppThreadLocalUtils.getUser();
        //1 查询实体
        ResponseResult<ApBehaviorEntry> behaviorResult = behaviorFeign.findByUserIdOrEquipmentId(user == null ? null : user.getId(), userSearchDto.getEquipmentId());
        if (behaviorResult.getCode() != 0) {
            CustException.cust(AppHttpCodeEnum.SERVER_ERROR, behaviorResult.getErrorMessage());
        }
        ApBehaviorEntry apBehaviorEntry = behaviorResult.getData();
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        // 查询当前用户的搜索列表  按照时间倒序 .limit(userSearchDto.getPageSize())
        List<ApUserSearch> userSearchList = mongoTemplate.find(Query.query(
                                Criteria.where("entryId").is(apBehaviorEntry.getId())
                                        .and("createdTime").lt(userSearchDto.getMinBehotTime()))
                        .with(Sort.by(Sort.Direction.DESC, "createdTime"))
                        .limit(size)
                , ApUserSearch.class);
        return ResponseResult.okResult(userSearchList);
    }

    /**
     * 删除搜索历史
     *
     * @param historySearchDto 搜索历史DTO
     * @return
     */
    @Override
    public ResponseResult delUserSearch(HistorySearchDTO historySearchDto) {
        // 查询实体ID
        ApUser user = AppThreadLocalUtils.getUser();
        ResponseResult<ApBehaviorEntry> behaviorEntryResult = behaviorFeign.findByUserIdOrEquipmentId(user == null ? null : user.getId(), historySearchDto.getEquipmentId());
        // 检查返回值
        if (!behaviorEntryResult.checkCode()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.REMOTE_SERVER_ERROR, "远程调用行为服务失败");
        }
        ApBehaviorEntry behaviorEntry = behaviorEntryResult.getData();
        if (behaviorEntry == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "行为实体数据不存在");
        }

        mongoTemplate.remove(Query.query(
                        Criteria.where("_id").is(historySearchDto.getId())
                                .and("entryId").is(behaviorEntry.getId()))
                , ApUserSearch.class);
        return ResponseResult.okResult();

    }
}
